#include <GL/glut.h>
#include <stdlib.h>
#include "loadtga.h"

#define MUNDO 4.0f
//Variables globales que guardan el tope de las coordenadas de mundo
float xMax, yMax;
GLuint texture;

void inicializar (void);
void redimensiona (int alto, int ancho);
void dibuja (void);
void teclado (unsigned char key, int cx, int cy);
void tecladoEspecial (int key, int cy, int cx);


int main(int argc, char** argv) {
   
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
   glutInitWindowSize (640, 480);
   glutInitWindowPosition (100, 105);
   glutCreateWindow (argv[0]);
   inicializar();
   
   glutDisplayFunc(dibuja);
   glutReshapeFunc(redimensiona);
   glutKeyboardFunc(teclado);
   glutSpecialFunc(tecladoEspecial);

   glutMainLoop();
   
   return 0;
}

void inicializar () {
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glEnable(GL_DEPTH_TEST);

   //Prendemos el modo de texturas
   glEnable(GL_TEXTURE_2D);
   //Esta es la interpolacion hiperbolica
   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

   //Cargamos la textura a memoria
   GLbyte *pBytes;
   GLint nWidth, nHeight, nComponents;
   GLenum format;

   glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_DECAL);
   glGenTextures(1, &texture);

   pBytes = gltLoadTGA("pumasLogo.tga", &nWidth, &nHeight, &nComponents, &format);
   glBindTexture(GL_TEXTURE_2D, texture);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes);
   free(pBytes);
}

void redimensiona (int ancho, int alto) {
   float aspect;
   
   glViewport (0, 0, ancho, alto);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   
   if (alto == 0){
   	  alto = 1;
   }
   
   aspect = (float) ancho / (float) alto;
   
   if (ancho <= alto) {
   	  glOrtho(-MUNDO, MUNDO, -MUNDO/aspect, MUNDO/aspect, -MUNDO, MUNDO);
   	  xMax = MUNDO;
   	  yMax = MUNDO / aspect;
   	  
   } else {
   	  glOrtho(-MUNDO * aspect, MUNDO * aspect, -MUNDO, MUNDO, -MUNDO, MUNDO);
   	  xMax = MUNDO * aspect;
   	  yMax = MUNDO;
   }
   
   
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

void teclado (unsigned char key, int cx, int cy) {
	
	switch (key) {
		case 27:
		   exit(0);
		break;
		
		default: break;
	}
	
	glutPostRedisplay();
}

void tecladoEspecial (int key, int cy, int cx) {
	
	switch (key) {
		case GLUT_KEY_UP:
		break;
		
		case GLUT_KEY_DOWN:
		break;
		
		case GLUT_KEY_LEFT:
		break;
		
		case GLUT_KEY_RIGHT:
		break;
		
		case GLUT_KEY_PAGE_UP:
		break;
		
		case GLUT_KEY_PAGE_DOWN:
		break;

		default: break;
	}
	
	glutPostRedisplay();
}
	

void dibuja (void) {
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	glColor3ub(236, 202, 0);
	glBindTexture(GL_TEXTURE_2D, texture);
	glBegin(GL_QUADS);
		glTexCoord2f(1.0, 1.0);
		glVertex3f(3.0, 3.0, 0.0);
		glTexCoord2f(1.0, 0.0f);
		glVertex3f(3.0, -3.0, 0.0f);
		glTexCoord2f(0.0, 0.0f);
		glVertex3f(-3.0, -3.0, 0.0);
		glTexCoord2f(0.0, 1.0);
		glVertex3f(-3.0, 3.0, 0.0);
	glEnd();
	    
	glutSwapBuffers();
}

